﻿function getUrlParam(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r!=null) return unescape(r[2]); return null; //返回参数值
}

$(document).on('ready', function(){
	var studentId = getUrlParam('studentId');
	if(/^\d{10}$/.test(studentId)){
		setCookie('studentId', studentId);
		$.mobile.changePage('#result',{
			transition:'slide'
		});
	}
});

$(document).on('pageshow', '#compare', function(){
	$('#submitBtn').unbind();
	var studentId = getCookie('studentId');
	if(studentId) $('#studentId').val(studentId);
	$('#submitBtn').on('tap', function(){
		var studentId = $('#studentId').val();
		if(!/^\d{10}$/.test(studentId)){
			$('#warnText').text('请输入正确的学号');
			$('#warningBtn').click();
		}else{	
			setCookie('studentId', studentId);
			$.mobile.changePage('#result',{
				transition:'slide'
			});
		}
	});
});

$(document).on('pageshow', '#result', function(){
	var studentId = getCookie('studentId');
	var top = $('#score').offset().top;
	$('#img').css('top', top - 15 +'px');
	$('#share').unbind();
	$('#share').on('click', function(){
		wx.ready(function(){
			wx.showOptionMenu();
			wx.hideMenuItems({
				menuList: [
					"menuItem:exposeArticle",
					"menuItem:setFont",
					"menuItem:dayMode",
					"menuItem:nightMode",
					"menuItem:refresh",
					"menuItem:profile",
					"menuItem:addContact",
					"menuItem:jsDebug",
					"menuItem:editTag",
					"menuItem:delete",
					"menuItem:copyUrl",
					"menuItem:originPage",
					"menuItem:readMode",
					"menuItem:openWithQQBrowser",
					"menuItem:openWithSafari",
					"menuItem:share:email",
					"menuItem:share:brand",
					"menuItem:share:qq",
					"menuItem:share:weiboApp"
				]
			});
		});
	});
	if(!/^\d{10}$/.test(studentId)){
		$.mobile.changePage('#compare',{
			transition:'slide'
		});
	}else{
		showLoader();
		$.ajax({
			url: './php/get_compare.php',
			type: 'post',
			dataType: 'json',
			data: '&studentId='+studentId,
			success:
				function(data){
					hideLoader();
					if(data.error){
						$.mobile.changePage('#compare',{
							transition:'slide'
						});
						$('#warnText').text(data.error);
						$('#warningBtn').click();
					}else{
						$('#name').text(data.name);
						$('#score').text(data.total);
						$('#percent').text(data.percent+'%');
						$('#title').text(data.title);
						$('#score2').text(data.total);
						$('#base').text(data.base);
						$('#gpa').text(data.score);
						$('#bonus').text(data.bonus);
						$('#img').attr("src", "./images/"+data.img);
						var url = 'http://withelper.itjesse.cn/wechat-compare/?studentId='+studentId;
						var title = '工大助手成绩大比拼';
						var img = 'http://withelper.itjesse.cn/wechat-compare/images/icon.jpg';
						var info = '我的战斗力是'+data.total+',被工大助手评为了'+data.title+'，快来看看！';
						
						wx.ready(function(){
							wx.onMenuShareTimeline({
								title: title, // 分享标题
								link: url, // 分享链接
								imgUrl: img, // 分享图标
								success: function () { 
									alert("分享成功！");
								},
								cancel: function () { 
									alert("为何不让大家看看呢？");
								}
							});
							wx.onMenuShareAppMessage({
								title: title, // 分享标题
								desc: info, // 分享描述
								link: url, // 分享链接
								imgUrl: img, // 分享图标
								type: 'link', // 分享类型,music、video或link，不填默认为link
								dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
								success: function () { 
									alert("分享成功！");
								},
								cancel: function () { 
									alert("为何不让大家看看呢？");
								}
							});
							wx.onMenuShareQQ({
								title: title, // 分享标题
								desc: info, // 分享描述
								link: url, // 分享链接
								imgUrl: img, // 分享图标
								success: function () { 
									alert("分享成功！");
								},
								cancel: function () { 
									alert("为何不让大家看看呢？");
								}
							});
							wx.onMenuShareWeibo({
								title: title, // 分享标题
								desc: info, // 分享描述
								link: url, // 分享链接
								imgUrl: img, // 分享图标
								success: function () { 
									alert("分享成功！");
								},
								cancel: function () { 
									alert("为何不让大家看看呢？");
								}
							});
						});
					}
				}
		});
	}
});